﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using System.ComponentModel;
using DevExpress.LookAndFeel;
using KNS.Invoice.UI.Settings;
using System.Reflection;

namespace KNS.Invoice.UI
{
    public class KNSFormBase : XtraForm
    {
        private IContainer components;
        public DefaultLookAndFeel defaultLookAndFeel;
        public int UId;
        public string UName;
        public string UCode;        

        public KNSFormBase()
        {
            this.InitializeComponent();
            SkinManager.EnableMdiFormSkins();
            SkinManager.EnableFormSkins();
            defaultLookAndFeel.LookAndFeel.SkinName = KNSCommonSettings.SkinName;
        }
        
        protected override void Dispose(bool disposing)
        {
            if (this.components != null && disposing)
                this.components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.defaultLookAndFeel = new DefaultLookAndFeel(this.components);
            //base.SuspendLayout();
        }

        public void SetFormActive(KNSFormBase frm, System.Windows.Forms.Panel pnl)
        {
            pnl.Controls.Clear();
            frm.TopLevel = false;
            frm.Visible = true;
            pnl.Controls.Add(frm);
            frm.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        }

        /// <summary>
        /// Displays a message box with the contents of given exception.
        /// Shows Message of exception and of inner exception.
        /// </summary>
        public static void ShowExceptionMessageBox(Exception ex)
        {
            ShowExceptionMessageBox(null, ex);
        }

        /// <summary>
        /// Displays a message box with the contents of given exception.
        /// Shows given message followed by Message of exception and of inner exception.
        /// </summary>
        public static void ShowExceptionMessageBox(string message, Exception ex)
        {
            // create the text to display (show up to one level of inner exception)
            var text = new StringBuilder();

            if (!string.IsNullOrEmpty(message))
            {
                text.AppendLine(message);
            }

            if (ex != null)
            {
                text.AppendLine(ex.Message);
            }

            if (ex != null && ex.InnerException != null)
            {
                text.AppendLine(ex.InnerException.Message);
            }

            // show the text
            KNSMessageBox.Show(text.ToString(), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }

        public static string GetAssemblyProduct()
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
            if (attributes.Length == 0)
            {
                return string.Empty;
            }

            return ((AssemblyProductAttribute)attributes[0]).Product;
        }
    }
}
